package org.carabiner;

import java.awt.Component;
import java.lang.reflect.InvocationTargetException;

import javax.swing.SwingUtilities;

import org.carabiner.harness.Harness;
import org.carabiner.harness.HarnessFactory;

/**
 * Main utility class that will create and show a carabiner harness for a
 * component
 * 
 * @author bjrady
 * 
 */
public class Carabiner
{
  private Carabiner()
  {
  }

  /**
   * Shows the harness for a component, this method can be called from a 
   * main() method within a test for a component.
   * 
   * @param component
   */
  public static void showHarness(final Component component)
  {
    Runnable runnable = new Runnable()
    {
      public void run()
      {
        Harness harness = HarnessFactory.createHarness(component);
        harness.showHarness();
      }
    };

    if (SwingUtilities.isEventDispatchThread())
    {
      runnable.run();
    }
    else
    {
      try
      {
        SwingUtilities.invokeAndWait(runnable);
      }
      catch (InterruptedException e)
      {
        throw new RuntimeException(e);
      }
      catch (InvocationTargetException e)
      {
        throw new RuntimeException(e);
      }
    }
  }
}
